package com.example.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.management.common.QueryPageParam;
import com.example.management.common.Result;
import com.example.management.entity.Details;
import com.example.management.service.DetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/details")
public class DetailsController {
    @Autowired
    private DetailsService detailsService;
    //查询
    @GetMapping("/list")
    public List<Details> list() { return detailsService.list(); }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Details details) {
        return detailsService.save(details) ? Result.success() : Result.fail();
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Details details) {
        return detailsService.updateById(details);
    }
    @PostMapping("/update")
    public Result update(@RequestBody Details details) {
        return detailsService.updateById(details) ? Result.success() : Result.fail();
    }
    @PostMapping("/updateBatch")
    public Result updateBatch(@RequestBody List<Details> list) {
        return detailsService.updateBatchById(list) ? Result.success() : Result.fail();
    }
    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOfMod(@RequestBody Details details) {
        return detailsService.saveOrUpdate(details);
    }
    //删除
    @GetMapping("/delete")
    public Result delete(String dno) {
        return detailsService.removeById(dno) ? Result.success() : Result.fail();
    }
    @GetMapping("/findByNo")
    public Result findByNo(String dno) {
        List<Details> list = detailsService.lambdaQuery().eq(Details::getDno, dno).list();
        return list.isEmpty() ? Result.fail() : Result.success(list);
    }
    @GetMapping("/findByPno")
    public Result findByPno(String pno) {
        List<Details> list = detailsService.lambdaQuery().eq(Details::getPno, pno).list();
        return list.isEmpty() ? Result.fail() : Result.success(list);
    }
    //查询（模糊、匹配）
    @PostMapping("/listP")
    public List<Details> listP(@RequestBody Details details) {
        LambdaQueryWrapper<Details> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Details::getPno, details.getPno());
        return detailsService.list(lambdaQueryWrapper);
    }
    @PostMapping("/listI")
    public List<Details> listI(@RequestBody Details details) {
        LambdaQueryWrapper<Details> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Details::getIno, details.getIno());
        return detailsService.list(lambdaQueryWrapper);
    }
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        System.out.println(query);
        System.out.println("num==" + query.getPageNum());
        System.out.println("size==" + query.getPageSize());
        HashMap param = query.getParam();
        String dno = (String) param.get("dno");
        String ino = (String) param.get("ino");
        String pno = (String) param.get("pno");

        Page<Details> page = new Page<>(query.getPageNum(), query.getPageSize());
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Details> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dno) && !"null".equals(dno)) {
            lambdaQueryWrapper.eq(Details::getDno, dno);
        }
        if(StringUtils.isNotBlank(ino) && !"null".equals(ino)) {
            lambdaQueryWrapper.eq(Details::getIno, ino);
        }
        if(StringUtils.isNotBlank(pno) && !"null".equals(pno)) {
            lambdaQueryWrapper.eq(Details::getPno, pno);
        }

        IPage<Details> data = detailsService.page(page, lambdaQueryWrapper);
        System.out.println("total==" + data.getTotal());

        return Result.success(data.getRecords(), data.getTotal());
    }
}